import 'package:example/pages/todo/todo_controller.dart';
import 'package:flutter/material.dart';
import 'package:getx_scaffold_ohos/getx_scaffold_ohos.dart';

import '../../models/todo_entity.dart';

class TodoItem extends StatelessWidget {
  final TodoEntity todo;
  final Function(TodoEntity todo) onEdit;
  final TodoController controller = Get.find();

  TodoItem({super.key, required this.todo, required this.onEdit});

  @override
  Widget build(BuildContext context) {
    return Dismissible(
      key: Key(todo.id.toString()),
      background: Container(color: Colors.red),
      confirmDismiss: (_)async {
        DialogX.to.showConfirmDialog(
          title: '确认删除',
          content: const Text('永久删除此待办事项？'),
          cancelText: '取消',
          confirmText: '确定删除',
        );
      },
      onDismissed: (_) {
        controller.deleteTodo(todo);
      },
      child: Card(
        elevation: 2,
        child: ListTile(
          title: Text(
            todo.title ?? '',
            style: TextStyle(
              decoration: todo.status == 1 ? TextDecoration.lineThrough: TextDecoration.none,
              color: todo.status == 1 ? Colors.grey : null,
            ),
          ),
          subtitle: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              if (todo.content?.isNotEmpty == true)
                Text(todo.content??'').paddingOnly(top: 4.h),
              Text(todo.dateStr??'', style: TextStyle(color: Colors.blue),),
            ],
          ),
          trailing: Checkbox(
            value: todo.status == 1,
            onChanged: (value) async{
              final updatedTodo = todo.copyWith(status: value! ? 1 : 0);
              controller.updateTodo(updatedTodo);
            },
          ),
          onTap: () => onEdit(todo),
        ),
      ),
    );
  }
}
